"""
-------------------------------
- File_Name: menus.py
- Date: 2025/3/7
- Author: yangzhide
- Email: xxzjyzd@126.com
- Description: 菜单模型
-------------------------------
"""

from typing import List
from pydantic import BaseModel, Field, model_validator, field_validator
from app.models.utils import StatusType,MenuType
from config import get_api_environment_var
from app.utils.common import to_camel_case

api_environment = get_api_environment_var()


"""
系统管理-菜单
"""

class SystemManageMenuButton(BaseModel):
    """按钮编码"""
    code: str | None = None
    """按钮描述"""
    desc: str | None = None


class ChildQuery(BaseModel):
    """参数key"""
    key: str
    """参数值"""
    value: str



class ChildElement(BaseModel):
    """SystemManageMenu"""
    """是否是常量路由，不需要登录权限就能访问"""
    constant: bool
    """创建时间"""
    create_time: str
    """唯一标识"""
    id: int
    """菜单名称"""
    menu_name: str
    """菜单类型，"1": "目录", "2": "菜单\""""
    menu_type: str
    """菜单顺序"""
    order: int
    """父级菜单id，没有父级id则为0"""
    parent_id: int
    """路由路径的查询参数"""
    query: List[ChildQuery] | None = None
    """路由名称"""
    route_name: str
    """路由路径"""
    route_path: str
    """启用状态，"1": "启用" , "2": "停用\""""
    status: str = Field(StatusType.enable)
    """更新者"""
    update_by: str | None = None
    """更新时间"""
    update_time: str | None = None
    """隐藏的路由需要激活的菜单，当前路由对应的菜单隐藏时，又需要高的菜单，例如：列表和列表详情，路由为列表详情时，高亮列表菜单"""
    active_menu: str | None = None
    """按钮集合"""
    buttons: List[SystemManageMenuButton] | None = None
    """子菜单"""
    children: List['ChildElement'] | None = None
    """路由组件，包含布局组件和页面组件，只有一级目录才有布局组件，一级菜单为布局组件和页面组件的组合"""
    component: str | None = None
    """创建者"""
    create_by: str | None = None
    """固定在页签的序号"""
    fixed_index_in_tab: int | None = None
    """菜单是否隐藏，一些带有查询参数的路由不应该在菜单中展示"""
    hide_in_menu: bool | None = None
    """外链"""
    href: str | None = None
    """i18n key，用于国际化"""
    i18_n_key: str | None = None
    """图标名称，iconify的图标名称或本地图标名称"""
    icon: str | None = None
    """图标类型，"1": "iconify 图标", "2": "本地图标\""""
    icon_type: str | None = None
    """是否缓存"""
    keep_alive: bool | None = None
    """是否支持多页签，不同查询参数的路由是否需要多页签展示，默认 false"""
    multi_tab: bool | None = None



class SystemManageMenuQuery(BaseModel):
    """参数key"""
    key: str
    """参数值"""
    value: str



class SystemManageMenuBase(BaseModel):
    """SystemManageMenu"""

    """是否是常量路由，不需要登录权限就能访问"""
    constant: bool = Field(False)
    """菜单名称"""
    menu_name: str
    """菜单类型，"1": "目录", "2": "菜单\""""
    menu_type: str = MenuType.catalog
    """菜单顺序"""
    order: int | None = None
    """父级菜单id，没有父级id则为0"""
    parent_id: int
    """路由路径的查询参数"""
    query: List[SystemManageMenuQuery] | None = None
    """路由名称"""
    route_name: str
    """路由路径"""
    route_path: str
    """启用状态，"1": "启用" , "2": "停用\""""
    status: str = Field(StatusType.enable)
    """隐藏的路由需要激活的菜单，当前路由对应的菜单隐藏时，又需要高的菜单，例如：列表和列表详情，路由为列表详情时，高亮列表菜单"""
    active_menu: str | None = None
    """按钮集合"""
    buttons: List[SystemManageMenuButton] | None = None
    """路由组件，包含布局组件和页面组件，只有一级目录才有布局组件，一级菜单为布局组件和页面组件的组合"""
    component: str | None = None
    """固定在页签的序号"""
    fixed_index_in_tab: int | None = None
    """菜单是否隐藏，一些带有查询参数的路由不应该在菜单中展示"""
    hide_in_menu: bool | None = None
    """外链"""
    href: str | None = None
    """i18n key，用于国际化"""
    i18n_key: str | None = None
    """图标名称，iconify的图标名称或本地图标名称"""
    icon: str | None = ""
    """图标类型，"1": "iconify 图标", "2": "本地图标\""""
    icon_type: str | None = None
    """是否缓存"""
    keep_alive: bool | None = None
    """是否支持多页签，不同查询参数的路由是否需要多页签展示，默认 false"""
    multi_tab: bool | None = None



class SystemManageMenuCreate(SystemManageMenuBase):
    """系统管理-菜单创建"""

    """创建者"""
    create_by: str | None = ""

    @field_validator("icon")
    @classmethod
    def check_icon(cls, value):
        if value == "":
            # 使用默认图标
            return api_environment.default_icon

    class Config:
        # 使用别名生成器, 自动将下划线命名转换为驼峰命名作为别名
        alias_generator = to_camel_case
        # 允许通过字段名或别名赋值
        populate_by_name = True


class SystemManageMenuUpdate(SystemManageMenuBase):
    """系统管理-菜单更新"""

    """更新者"""
    update_by: str | None = None

    class Config:
        # 使用别名生成器, 自动将下划线命名转换为驼峰命名作为别名
        alias_generator = to_camel_case
        # 允许通过字段名或别名赋值
        populate_by_name = True


class SystemManageMenuOut(SystemManageMenuBase):
    """系统管理-返回单个菜单"""


    """唯一标识"""
    id: int
    """创建时间"""
    create_time: str
    """创建者"""
    create_by: str | None = Field(None)
    """更新时间"""
    update_time: str | None = Field(None)
    """更新者"""
    update_by: str | None = Field(None)

    class Config:
        alias_generator = to_camel_case
        populate_by_name = True

class RoleMenuHome(BaseModel):
    """菜单字典列表————角色首页选项"""
    menu_name: str
    route_name: str
    class Config:
        alias_generator = to_camel_case
        populate_by_name = True





class SystemManageMenuOutList(SystemManageMenuOut):
    """系统管理-返回菜单列表"""

    update_by: str | None = Field(None, serialization_alias="updateBy")
    """子菜单"""
    children: List["SystemManageMenuOutList"] | None = None
